package main

import "fmt"

/*
给定一个有序数组arr，一个正数aim
1. 求累加和为aim的，所有不同二元组
2. 求累加和为aim的，所有不同三元组
 */

func getTupleSum(arr []int, aim int) [][2]int {
    var res [][2]int
    Map := make(map[int]int, 16)
    for _, v := range arr {
        Map[aim-v] = v
    }
    for _, v := range arr {
        r, ok := Map[v]
        if ok {
            delete(Map, r)
            delete(Map, v)
            res = append(res, [2]int{v, r})
        }
    }
	return res
}

func getTupleSum2(arr []int, aim int) [][2]int {
    var res [][2]int
    var cur int
    left := 0
    right := len(arr)-1
    for left <= right {
        cur = aim - arr[left]
        for cur < arr[right] {
            right--
        }
        //此时right处为目标值
        res = append(res, [2]int{arr[left], arr[right]})
        right--
        left++
    }
    return res
}

//TODO
func getTriple(arr []int, aim int) [][3]int {

    return nil
}

func main() {
    arr := []int{-5,1,3,5,7,8,9,10,11,15,16,17,18}
//     res := getTupleSum2(arr, 10)
    res := getTriple(arr, 15)
    for _, v := range res {
        fmt.Println(v)
    }
    fmt.Println(len(res))
}
